package com.ccit.utils;

import com.ccit.domain.AttendanceType;
import com.ccit.service.AttendanceService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Component
public class DataAddTask implements Job {
    private final AttendanceService attendanceService;

    // 通过构造函数注入AttendanceService
    @Autowired
    public DataAddTask(AttendanceService attendanceService) {
        this.attendanceService = attendanceService;
    }

    // 无参构造函数
    public DataAddTask() {
        this.attendanceService = null;
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        if (attendanceService == null) {
            throw new JobExecutionException("AttendanceService is not injected.");
        }

//        LocalDate currentDate = LocalDate.now();
//        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//        String checkDate = currentDate.format(dateFormatter);

        String checkDate = "2023-11-07";


        System.out.println(checkDate);

        List<AttendanceType> attendanceList = attendanceService.selectByEmployeeIdAndDate();
        System.out.println(attendanceList);

        for (AttendanceType attendance : attendanceList) {
            int employeeId = attendance.getEmployeeId();
            String employeeName = attendance.getEmployeeName();
            System.out.println("Employee ID: " + employeeId);
            AttendanceType attendanceType = new AttendanceType();
            attendanceType.setEmployeeId(employeeId);
            attendanceType.setEmployeeName(employeeName);
//            attendanceType.setCheckIn(checkDate +" "+"00:00:00");
//            attendanceType.setCheckOut(checkDate +" "+"00:00:00");
            attendanceType.setCheckIn(checkDate +" "+"08:30:00");
            attendanceType.setCheckOut(checkDate +" "+"17:35:00");

            attendanceType.setCheckDate(checkDate );
            attendanceType.setStatus("正常");
            System.out.println(attendanceType);
            attendanceService.add(attendanceType);
        }
    }
}